﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class Controler
{
    public static Controler Instance = new Controler();

    //  Camera's Parameter
    public Camera camera{get{return m_Camera;}}
    Camera m_Camera;
    Vector3 m_CameraPosition;
    bool m_IsFreeCamera = false;
    float m_CameraZoom = -1;
    Ship m_FoucsShip;
    GameObject m_FoucsedObject;
    int m_ShipOprate = 0;//是否正在操控舰船
    float m_CameraInitCount = 0;
    float m_CameraInitCountMax = 0.5f;
    bool wasZoomingLastFrame = false;
    Vector2[] lastZoomPositions;


    GameObject m_TargetIcon;
    //  Other's 
    public bool editMode { get; set; } = false;

    public void InitSystem()
    {
        InitWorld();
    }
    public void InitWorld(Scene scene, LoadSceneMode mode)
    {
        InitWorld();
    }

    public void InitWorld()
    {
        m_Camera = Camera.main;
        m_Camera.farClipPlane = 1000000;
        m_TargetIcon = new GameObject("FoucsTarget");
        var spriteRender =  m_TargetIcon.AddComponent<SpriteRenderer>();
        spriteRender.sprite = Utils.LoadSpriteByResouce("Textures/UI/Common/Target");
    }
    public void FocusObject(GameObject obj)
    {
        m_FoucsedObject = obj;
    }
    public void FocusShip( Ship ship)
    {
        m_FoucsShip = ship;
    }
    public void Update()  
    {
        updateCamera();
        if(m_FoucsShip)
        {
            var foucsPosition = m_Camera.ScreenToWorldPoint(Input.mousePosition);
            foucsPosition.z = 0;
            m_TargetIcon.transform.position = foucsPosition;
            m_FoucsShip.Arm(foucsPosition);
        }
        else if(m_FoucsedObject)
        {
            var foucsPosition = m_Camera.ScreenToWorldPoint(Input.mousePosition);
            foucsPosition.z = 0;
            m_TargetIcon.transform.position = foucsPosition;
        }
    } 
    void updateCamera()
    {
        GameObject foucsedObject = m_FoucsedObject;
        if (!m_FoucsShip && !m_FoucsedObject)
            return;
        if (m_FoucsShip)
            foucsedObject = m_FoucsShip.gameObject;
        if (m_Camera)
        {
            var pos = foucsedObject.transform.position;
            pos.z = -10;
            m_Camera.transform.position = pos;
        }
        CameraZoom();
        if (editMode)
            return;
        if (!m_IsFreeCamera)
            m_CameraPosition = foucsedObject.transform.position;
        //var particalShpe = EffectBackground.shape;
        //particalShpe.position = m_CameraPosition;
        m_CameraPosition.z = -1000;
#if UNITY_ANDROID
        var offset = new Vector3(0,0,0);
#else
        var mousePosition = Input.mousePosition;
        float xS = mousePosition.x / UnityEngine.Screen.width * 2 - 1;
        float yS = mousePosition.y / UnityEngine.Screen.height * 2 - 1;
        var screenPosScale = new Vector3(xS, yS);
        var offset = screenPosScale * ((m_Camera.orthographicSize * Mathf.Sin(screenPosScale.magnitude)) * 0.8f);
#endif
        m_Camera.transform.position = m_CameraPosition + offset;
    }
    void CameraZoom()
    {
#if UNITY_ANDROID
        if(Input.touchCount == 2 && m_ShipOprate == 0)
        {
            Vector2[] newPositions = new Vector2[] { 
                new Vector2(Input.GetTouch(0).position.x / Screen.width, Input.GetTouch(0).position.y / Screen.height),
                new Vector2(Input.GetTouch(1).position.x / Screen.width, Input.GetTouch(1).position.y / Screen.height)};

            if (!wasZoomingLastFrame)
            {
                lastZoomPositions = newPositions;
                wasZoomingLastFrame = true;
            }
            else
            {
                // Zoom based on the distance between the new positions compared to the 
                // distance between the previous positions.
                float newDistance = Vector2.Distance(newPositions[0], newPositions[1]);
                float oldDistance = Vector2.Distance(lastZoomPositions[0], lastZoomPositions[1]);
                float offset = newDistance - oldDistance;
        
                m_CameraZoom = Mathf.Clamp(m_CameraZoom - offset * m_CameraZoom, 2, 30);
                lastZoomPositions = newPositions;
            }
        }
        else
        {
            wasZoomingLastFrame = false;
        }
        if (m_CameraInitCount > m_CameraInitCountMax &&
            Mathf.Abs(m_Camera.orthographicSize - m_CameraZoom) > 0.1f)
        {
            m_Camera.orthographicSize = Mathf.Lerp(m_Camera.orthographicSize, m_CameraZoom, Time.deltaTime * 3);
        }
#else
        if (m_Camera != null && !EventSystem.current.IsPointerOverGameObject())
        {
            m_CameraZoom = Mathf.Clamp(m_CameraZoom - Input.GetAxis("Mouse ScrollWheel") * m_CameraZoom, 2, 30);
            if (m_CameraInitCount > m_CameraInitCountMax)
            {
                m_Camera.orthographicSize = Mathf.Lerp(m_Camera.orthographicSize, m_CameraZoom, Time.deltaTime * 3);
            }
        } 
#endif
        m_CameraInitCount += Time.deltaTime;
    }
}